# 手动指定SPDK库，因为pkg-config没有找到spdk包
set(XSAN_SPDK_LIBRARIES
    spdk_util
    spdk_log
    spdk_thread
    spdk_event
    spdk_event_bdev
    spdk_event_nvmf
    spdk_event_sock
    spdk_event_vhost_blk
    spdk_event_vhost_scsi
    spdk_event_vmd
    spdk_event_accel
    spdk_event_fsdev
    spdk_event_iobuf
    spdk_event_iscsi
    spdk_event_keyring
    spdk_event_nbd
    spdk_event_scheduler
    spdk_event_scsi
    spdk_bdev
    spdk_bdev_aio
    spdk_bdev_delay
    spdk_bdev_error
    spdk_bdev_ftl
    spdk_bdev_gpt
    spdk_bdev_lvol
    spdk_bdev_malloc
    spdk_bdev_null
    spdk_bdev_nvme
    spdk_bdev_passthru
    spdk_bdev_raid
    spdk_bdev_split
    spdk_bdev_virtio
    spdk_bdev_zone_block
    spdk_blob
    spdk_blob_bdev
    spdk_blobfs
    spdk_blobfs_bdev
    spdk_conf
    spdk_dma
    spdk_env_dpdk
    spdk_env_dpdk_rpc
    spdk_event_accel
    spdk_event_bdev
    spdk_event_fsdev
    spdk_event_iobuf
    spdk_event_iscsi
    spdk_event_keyring
    spdk_event_nbd
    spdk_event_nvmf
    spdk_event_scheduler
    spdk_event_scsi
    spdk_event_sock
    spdk_event_vhost_blk
    spdk_event_vhost_scsi
    spdk_event_vmd
    spdk_fsdev
    spdk_fsdev_aio
    spdk_ftl
    spdk_fuse_dispatcher
    spdk_init
    spdk_ioat
    spdk_iscsi
    spdk_json
    spdk_jsonrpc
    spdk_keyring
    spdk_keyring_file
    spdk_keyring_linux
    spdk_lvol
    spdk_nbd
    spdk_notify
    spdk_nvme
    spdk_nvmf
    spdk_rpc
    spdk_scheduler_dpdk_governor
    spdk_scheduler_dynamic
    spdk_scheduler_gscheduler
    spdk_scsi
    spdk_sock
    spdk_sock_posix
    spdk_trace
    spdk_trace_parser
    spdk_ut
    spdk_ut_mock
    spdk_vfio_user
    spdk_vhost
    spdk_virtio
    spdk_vmd
    spdk_accel
    spdk_accel_error
    spdk_accel_ioat
)
cmake_minimum_required(VERSION 3.16)
project(xsan VERSION 1.0.0 LANGUAGES C)

# 设置 C 标准
set(CMAKE_C_STANDARD 99)
set(CMAKE_C_STANDARD_REQUIRED ON)

# 编译选项
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra -Wno-unused-parameter")
set(CMAKE_C_FLAGS_DEBUG "-g -O0 -DDEBUG")
set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")

# 全局包含目录
include_directories(${CMAKE_SOURCE_DIR})
include_directories(include)
include_directories(src/include)

# 线程库
find_package(Threads REQUIRED)

# Option to enable SPDK support
option(XSAN_ENABLE_SPDK "Enable SPDK integration" ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON) # Good for IDEs






# 查找系统 rocksdb 头文件和库
find_path(ROCKSDB_INCLUDE_DIR NAMES rocksdb/c.h PATHS /usr/include /usr/local/include)
find_library(ROCKSDB_LIBRARY NAMES rocksdb PATHS /usr/lib /usr/local/lib)
if(ROCKSDB_INCLUDE_DIR AND ROCKSDB_LIBRARY)
    message(STATUS "Found system RocksDB include: ${ROCKSDB_INCLUDE_DIR}")
    message(STATUS "Found system RocksDB library: ${ROCKSDB_LIBRARY}")
    include_directories(SYSTEM ${ROCKSDB_INCLUDE_DIR})
    set(XSAN_ROCKSDB_LIBRARIES ${ROCKSDB_LIBRARY} CACHE INTERNAL "RocksDB Libraries")
else()
    message(FATAL_ERROR "System RocksDB not found. Please install librocksdb-dev.")
endif()

# 查找并集成 NUMA 库
find_path(NUMA_INCLUDE_DIR NAMES numa.h PATHS /usr/include /usr/local/include)
find_library(NUMA_LIBRARY NAMES numa PATHS /usr/lib /usr/local/lib)
if(NUMA_INCLUDE_DIR AND NUMA_LIBRARY)
    message(STATUS "Found NUMA include: ${NUMA_INCLUDE_DIR}")
    message(STATUS "Found NUMA library: ${NUMA_LIBRARY}")
    include_directories(SYSTEM ${NUMA_INCLUDE_DIR})
    set(XSAN_NUMA_LIBRARIES ${NUMA_LIBRARY} CACHE INTERNAL "NUMA Libraries")
else()
    message(WARNING "NUMA library not found, performance may be affected.")
endif()


# 构建 SPDK 子模块（假设使用 make，需提前配置好 SPDK 环境）
add_custom_target(MakeSPDK
    COMMAND make -C ${CMAKE_SOURCE_DIR}/src/spdk
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/src/spdk
    COMMENT "Building SPDK submodule"
)


# xsan 主包依赖 SPDK 构建（RocksDB 由系统提供，无需自定义构建目标）
add_custom_target(xsan_all DEPENDS MakeSPDK
    COMMENT "Build SPDK and XSAN together (RocksDB from system)"
)

# SPDK 头文件和库路径（需先在 src/spdk 编译生成 build/lib 和 include）
include_directories(src/spdk/include)
link_directories(src/spdk/build/lib)

# SPDK 编译标志
if(XSAN_ENABLE_SPDK)
    set(XSAN_SPDK_CFLAGS "-DSPDK_APP -DSPDK_APP_RPC_SERVER -DSPDK_APP_RPC_CLIENT" CACHE INTERNAL "SPDK Compiler Flags")
    set(XSAN_SPDK_LIBRARY_DIRS "src/spdk/build/lib" CACHE INTERNAL "SPDK Library Directories")
    add_compile_options(${XSAN_SPDK_CFLAGS})
    include_directories(SYSTEM src/spdk/include)
endif()


# 添加头文件路径（仅 SPDK，RocksDB 由系统提供）
include_directories(src/spdk/include)

# 查找DPDK库 - 支持版本25的库
find_library(DPDK_EAL_LIBRARY NAMES rte_eal rte_eal.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_MEMPOOL_LIBRARY NAMES rte_mempool rte_mempool.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_RING_LIBRARY NAMES rte_ring rte_ring.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_MEMORY_LIBRARY NAMES rte_memory rte_memory.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_PCI_LIBRARY NAMES rte_pci rte_pci.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_BUS_LIBRARY NAMES rte_bus rte_bus.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_BUS_PCI_LIBRARY NAMES rte_bus_pci rte_bus_pci.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_DEVICE_LIBRARY NAMES rte_device rte_device.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_INTR_LIBRARY NAMES rte_intr rte_intr.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_TIMER_LIBRARY NAMES rte_timer rte_timer.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_KVARGS_LIBRARY NAMES rte_kvargs rte_kvargs.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_METRICS_LIBRARY NAMES rte_metrics rte_metrics.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_TELEMETRY_LIBRARY NAMES rte_telemetry rte_telemetry.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_ETHDEV_LIBRARY NAMES rte_ethdev rte_ethdev.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_NET_LIBRARY NAMES rte_net rte_net.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_MBUF_LIBRARY NAMES rte_mbuf rte_mbuf.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_HASH_LIBRARY NAMES rte_hash rte_hash.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_LPM_LIBRARY NAMES rte_lpm rte_lpm.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_ACL_LIBRARY NAMES rte_acl rte_acl.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_POWER_LIBRARY NAMES rte_power rte_power.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_CFGFILE_LIBRARY NAMES rte_cfgfile rte_cfgfile.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_CMDLINE_LIBRARY NAMES rte_cmdline rte_cmdline.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_REORDER_LIBRARY NAMES rte_reorder rte_reorder.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_SCHEDULER_LIBRARY NAMES rte_scheduler rte_scheduler.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_PIPELINE_LIBRARY NAMES rte_pipeline rte_pipeline.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_PORT_LIBRARY NAMES rte_port rte_port.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_TABLE_LIBRARY NAMES rte_table rte_table.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_PDUMP_LIBRARY NAMES rte_pdump rte_pdump.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_RAWDEV_LIBRARY NAMES rte_rawdev rte_rawdev.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_VHOST_LIBRARY NAMES rte_vhost rte_vhost.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_IPSEC_LIBRARY NAMES rte_ipsec rte_ipsec.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_SECURITY_LIBRARY NAMES rte_security rte_security.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_COMPRESS_LIBRARY NAMES rte_compressdev rte_compressdev.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_REGEX_LIBRARY NAMES rte_regexdev rte_regexdev.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_RCU_LIBRARY NAMES rte_rcu rte_rcu.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_STACK_LIBRARY NAMES rte_stack rte_stack.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_NODE_LIBRARY NAMES rte_node rte_node.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_GRAPH_LIBRARY NAMES rte_graph rte_graph.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_BPF_LIBRARY NAMES rte_bpf rte_bpf.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_GPUDEV_LIBRARY NAMES rte_gpudev rte_gpudev.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_DMA_LIBRARY NAMES rte_dmadev rte_dmadev.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_DISTRIBUTOR_LIBRARY NAMES rte_distributor rte_distributor.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_JOBSTATS_LIBRARY NAMES rte_jobstats rte_jobstats.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_KNI_LIBRARY NAMES rte_kni rte_kni.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_LATENCY_STATS_LIBRARY NAMES rte_latencystats rte_latencystats.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_MEMBER_LIBRARY NAMES rte_member rte_member.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_EFD_LIBRARY NAMES rte_efd rte_efd.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_BBDEV_LIBRARY NAMES rte_bbdev rte_bbdev.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_FIB_LIBRARY NAMES rte_fib rte_fib.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_RIB_LIBRARY NAMES rte_rib rte_rib.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_GRO_LIBRARY NAMES rte_gro rte_gro.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_GSO_LIBRARY NAMES rte_gso rte_gso.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_GTP_LIBRARY NAMES rte_gtp rte_gtp.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_IP_FRAG_LIBRARY NAMES rte_ip_frag rte_ip_frag.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_COMMON_QAT_LIBRARY NAMES rte_common_qat rte_common_qat.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_COMMON_MLX5_LIBRARY NAMES rte_common_mlx5 rte_common_mlx5.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_CRYPTO_IPSEC_MB_LIBRARY NAMES rte_crypto_ipsec_mb rte_crypto_ipsec_mb.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_CRYPTO_MLX5_LIBRARY NAMES rte_crypto_mlx5 rte_crypto_mlx5.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_SECURITY_LIBRARY NAMES rte_security rte_security.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_METER_LIBRARY NAMES rte_meter rte_meter.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(DPDK_BUS_VDEV_LIBRARY NAMES rte_bus_vdev rte_bus_vdev.so.25 PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)

# 只包含找到的库，避免NOTFOUND错误
set(XSAN_DPDK_LIBRARIES "")
if(DPDK_EAL_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_EAL_LIBRARY})
endif()
if(DPDK_MEMPOOL_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_MEMPOOL_LIBRARY})
endif()
if(DPDK_RING_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_RING_LIBRARY})
endif()
if(DPDK_MEMORY_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_MEMORY_LIBRARY})
endif()
if(DPDK_PCI_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_PCI_LIBRARY})
endif()
if(DPDK_BUS_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_BUS_LIBRARY})
endif()
if(DPDK_BUS_PCI_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_BUS_PCI_LIBRARY})
endif()
if(DPDK_DEVICE_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_DEVICE_LIBRARY})
endif()
if(DPDK_INTR_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_INTR_LIBRARY})
endif()
if(DPDK_TIMER_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_TIMER_LIBRARY})
endif()
if(DPDK_KVARGS_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_KVARGS_LIBRARY})
endif()
if(DPDK_METRICS_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_METRICS_LIBRARY})
endif()
if(DPDK_TELEMETRY_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_TELEMETRY_LIBRARY})
endif()
if(DPDK_ETHDEV_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_ETHDEV_LIBRARY})
endif()
if(DPDK_NET_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_NET_LIBRARY})
endif()
if(DPDK_MBUF_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_MBUF_LIBRARY})
endif()
if(DPDK_HASH_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_HASH_LIBRARY})
endif()
if(DPDK_LPM_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_LPM_LIBRARY})
endif()
if(DPDK_ACL_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_ACL_LIBRARY})
endif()
if(DPDK_POWER_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_POWER_LIBRARY})
endif()
if(DPDK_CFGFILE_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_CFGFILE_LIBRARY})
endif()
if(DPDK_CMDLINE_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_CMDLINE_LIBRARY})
endif()
if(DPDK_REORDER_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_REORDER_LIBRARY})
endif()
if(DPDK_SCHEDULER_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_SCHEDULER_LIBRARY})
endif()
if(DPDK_PIPELINE_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_PIPELINE_LIBRARY})
endif()
if(DPDK_PORT_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_PORT_LIBRARY})
endif()
if(DPDK_TABLE_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_TABLE_LIBRARY})
endif()
if(DPDK_PDUMP_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_PDUMP_LIBRARY})
endif()
if(DPDK_RAWDEV_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_RAWDEV_LIBRARY})
endif()
if(DPDK_VHOST_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_VHOST_LIBRARY})
endif()
if(DPDK_IPSEC_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_IPSEC_LIBRARY})
endif()
if(DPDK_SECURITY_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_SECURITY_LIBRARY})
endif()
if(DPDK_COMPRESS_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_COMPRESS_LIBRARY})
endif()
if(DPDK_REGEX_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_REGEX_LIBRARY})
endif()
if(DPDK_RCU_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_RCU_LIBRARY})
endif()
if(DPDK_STACK_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_STACK_LIBRARY})
endif()
if(DPDK_NODE_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_NODE_LIBRARY})
endif()
if(DPDK_GRAPH_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_GRAPH_LIBRARY})
endif()
if(DPDK_BPF_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_BPF_LIBRARY})
endif()
if(DPDK_GPUDEV_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_GPUDEV_LIBRARY})
endif()
if(DPDK_DMA_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_DMA_LIBRARY})
endif()
if(DPDK_DISTRIBUTOR_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_DISTRIBUTOR_LIBRARY})
endif()
if(DPDK_JOBSTATS_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_JOBSTATS_LIBRARY})
endif()
if(DPDK_KNI_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_KNI_LIBRARY})
endif()
if(DPDK_LATENCY_STATS_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_LATENCY_STATS_LIBRARY})
endif()
if(DPDK_MEMBER_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_MEMBER_LIBRARY})
endif()
if(DPDK_EFD_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_EFD_LIBRARY})
endif()
if(DPDK_BBDEV_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_BBDEV_LIBRARY})
endif()
if(DPDK_FIB_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_FIB_LIBRARY})
endif()
if(DPDK_RIB_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_RIB_LIBRARY})
endif()
if(DPDK_GRO_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_GRO_LIBRARY})
endif()
if(DPDK_GSO_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_GSO_LIBRARY})
endif()
if(DPDK_GTP_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_GTP_LIBRARY})
endif()
if(DPDK_IP_FRAG_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_IP_FRAG_LIBRARY})
endif()
if(DPDK_COMMON_QAT_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_COMMON_QAT_LIBRARY})
endif()
if(DPDK_COMMON_MLX5_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_COMMON_MLX5_LIBRARY})
endif()
if(DPDK_CRYPTO_IPSEC_MB_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_CRYPTO_IPSEC_MB_LIBRARY})
endif()
if(DPDK_CRYPTO_MLX5_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_CRYPTO_MLX5_LIBRARY})
endif()
if(DPDK_SECURITY_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_SECURITY_LIBRARY})
endif()
if(DPDK_METER_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_METER_LIBRARY})
endif()
if(DPDK_BUS_VDEV_LIBRARY)
    list(APPEND XSAN_DPDK_LIBRARIES ${DPDK_BUS_VDEV_LIBRARY})
endif()

if(XSAN_DPDK_LIBRARIES)
    message(STATUS "Found DPDK libraries: ${XSAN_DPDK_LIBRARIES}")
else()
    message(WARNING "No DPDK libraries found. SPDK may not work correctly.")
endif()

# 查找OpenSSL库（用于加密功能）
find_library(OPENSSL_LIBRARY NAMES ssl crypto PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
if(OPENSSL_LIBRARY)
    message(STATUS "Found OpenSSL library: ${OPENSSL_LIBRARY}")
    set(XSAN_OPENSSL_LIBRARIES ${OPENSSL_LIBRARY} CACHE INTERNAL "OpenSSL Libraries")
else()
    message(WARNING "OpenSSL library not found, crypto features may not work.")
endif()

# 查找 ISA-L 库
find_library(ISAL_LIBRARY NAMES isal PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
find_library(ISAL_CRYPTO_LIBRARY NAMES isal_crypto PATHS /usr/lib /usr/local/lib /usr/lib/x86_64-linux-gnu)
if(ISAL_LIBRARY)
    message(STATUS "Found ISA-L library: ${ISAL_LIBRARY}")
    set(XSAN_ISAL_LIBRARIES ${ISAL_LIBRARY} CACHE INTERNAL "ISA-L Libraries")
    if(ISAL_CRYPTO_LIBRARY)
        list(APPEND XSAN_ISAL_LIBRARIES ${ISAL_CRYPTO_LIBRARY})
        message(STATUS "Found ISA-L crypto library: ${ISAL_CRYPTO_LIBRARY}")
    endif()
    
    # ISA-L 库链接，不添加特殊链接选项
    
else()
    message(WARNING "ISA-L library not found, some SPDK accel features may not work.")
endif()

# 其他依赖（如 json-c）仍可用 pkg-config 查找
find_package(PkgConfig REQUIRED)
pkg_search_module(JSON_C REQUIRED json-c)
if(JSON_C_FOUND)
    message(STATUS "Found json-c: ${JSON_C_LIBRARIES}")
    message(STATUS "json-c Include Dirs: ${JSON_C_INCLUDE_DIRS}")
    set(XSAN_JSON_C_INCLUDE_DIRS ${JSON_C_INCLUDE_DIRS} CACHE INTERNAL "json-c Include Directories")
    set(XSAN_JSON_C_LIBRARIES ${JSON_C_LIBRARIES} CACHE INTERNAL "json-c Libraries")
    set(XSAN_JSON_C_LIBRARY_DIRS ${JSON_C_LIBRARY_DIRS} CACHE INTERNAL "json-c Library Directories")
    include_directories(SYSTEM ${XSAN_JSON_C_INCLUDE_DIRS})
else()
    message(FATAL_ERROR "json-c library not found by pkg-config. Please install json-c.")
endif()

# === 自动修复部分结束 ===

# Add all relevant source directories now
add_subdirectory(src/utils)
add_subdirectory(src/common)
add_subdirectory(src/network) # Added network
add_subdirectory(src/core)    # Added core (for spdk_manager)
add_subdirectory(src/bdev)    # Added bdev
add_subdirectory(src/io)      # Added io (for xsan_io)
add_subdirectory(src/replication) # Added replication (for replicated IO contexts etc.)
add_subdirectory(src/metadata) # Added metadata (for RocksDB store wrapper)
add_subdirectory(src/storage) # Added storage (for disk_manager, volume_manager)
add_subdirectory(src/vhost)   # Added vhost (for XSAN vbdev and vhost integration)
add_subdirectory(src/nvmf)    # Added NVMe-oF target module
add_subdirectory(src/main)    # Main executable likely defined here
add_subdirectory(tests)
add_subdirectory(src/cluster)

# Display configuration information
message(STATUS "")
message(STATUS "=== XSAN Configuration (Simplified) ===")
message(STATUS "Version: ${PROJECT_VERSION}")
message(STATUS "C Compiler: ${CMAKE_C_COMPILER}")
message(STATUS "C Flags: ${CMAKE_C_FLAGS}")
message(STATUS "")
message(STATUS "Building basic modules only:")
message(STATUS "  - Utils module: ON")
message(STATUS "  - Common module: ON")
message(STATUS "  - Tests: ON")
message(STATUS "=======================================")
message(STATUS "")
